/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c) 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * @original author: jin Chen 
 * @author: jin Chen 
 * @date: May 28, 2008$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.common;

import edu.psu.geovista.app.dataloader.DfMetaData;
import edu.psu.geovista.app.dataloader.v1.components.DefaultNCIDataSource;
import edu.psu.geovista.app.dataloader.v1.components.WarningException;
import edu.psu.geovista.data.model.BasicMetadataModel;

import java.io.FileNotFoundException;

public class DataUtils {
    private static final boolean DEBUG=true;
    
    /**
     *  
     *  
     * @param fn
     * @return
     * @throws java.io.FileNotFoundException
     * @throws edu.psu.geovista.app.dataloader.v1.components.WarningException
     * @throws Exception
     */
    public  static DfMetaData loadMetaData(String fn)throws WarningException,Exception{
        //MetaData metaData=new MetaData();
        Object[] raw =new Object[0];
        try {
            raw = DefaultNCIDataSource.loadCSVMetaFile(fn);
        } catch (FileNotFoundException e) {
            if (DataUtils.DEBUG) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }
            throw e;
        }
        catch(IllegalArgumentException ie){
            //it means the file is not a cvs file, just ignore the meta data file
            throw new WarningException(ie.getMessage()  +"\n The meta data file is ignored");
        }

        if(raw==null ||raw.length==0 ){
            String error="Unable to find or parse meta file "+fn+". \n Some functions will be turned off. Please ask your data provider. ";
            //dp{
            if (DataUtils.DEBUG){
                System.out.println(error);
            }//dp}
            throw new WarningException(error);// 
        }

        BasicMetadataModel model=new BasicMetadataModel();
        model.feedCVSMetaData(raw);
        //model.sort(3);

        DfMetaData meta=new DfMetaData();
        meta.setModel(model);
        meta.process();
        /*try {
            meta.setDatamodel(model);
        } catch (UnsupportedOperationException e) {
            String msg = e.getMessage();
            msg="Found a problem as below in meta file: "+fn+" "+"\n\n" +"        "+msg+". \n\n Some functions will be turned off. Please check the file. ";
            throw new WarningException(msg);
        }*/
        return meta;

    }

    

    public static void createSimpleMetaFile(String[] observationNames){

    }
}
